package com.haha.extend.HeadBar;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.Long.library.R;

/**
 * Created by Administrator on 2016/6/24 0024.
 */
public abstract class BaseHeaderView extends LinearLayout{
    protected  Context context;
    private LinearLayout headLeft,headCenter,headRight;
    private boolean isShowLeft,isShowCenter,isShowRight;
    private int headViewLeftPadding,headViewRightPadding;
    public BaseHeaderView(Context context) {
        this(context,null);
    }

    public BaseHeaderView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public BaseHeaderView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        getAttrs(attrs);
        findView();
        initView();
    }

    protected void getAttrs(AttributeSet attrs){
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.BaseHeaderView);
        isShowLeft = ta.getBoolean(R.styleable.BaseHeaderView_showLeft,true);
        isShowCenter = ta.getBoolean(R.styleable.BaseHeaderView_showCenter,true);
        isShowRight = ta.getBoolean(R.styleable.BaseHeaderView_showRight,true);
        headViewLeftPadding = ta.getDimensionPixelSize(R.styleable.BaseHeaderView_headerViewLeftPadding,0);
        headViewRightPadding = ta.getDimensionPixelSize(R.styleable.BaseHeaderView_headerViewRightPadding,0);
        ta.recycle();
    }

    protected  void findView(){
        View headView = View.inflate(context,R.layout.widget_header_view,this);
        headLeft = (LinearLayout)headView.findViewById(R.id.header_left);
        headCenter = (LinearLayout)headView.findViewById(R.id.header_center);
        headRight = (LinearLayout)headView.findViewById(R.id.header_right);
    }

    public abstract  View getLeftView();
    public abstract  View getCenterView();
    public abstract  View getRightView();

    protected void initView(){
        headLeft.setVisibility(isShowLeft ? VISIBLE :INVISIBLE);
        headCenter.setVisibility(isShowCenter ? VISIBLE :INVISIBLE);
        headRight.setVisibility(isShowRight ? VISIBLE :INVISIBLE);

        headLeft.setPadding(headViewLeftPadding,0,0,0);
        headRight.setPadding(0,0,headViewRightPadding,0);

        if(getLeftView() != null)headLeft.addView(getLeftView());
        if(getCenterView() != null)headCenter.addView(getCenterView());
        if(getRightView() != null)headRight.addView(getRightView());

        headLeft.setOnClickListener(onClickListener);
        headCenter.setOnClickListener(onClickListener);
        headRight.setOnClickListener(onClickListener);
    }

    public interface OnHeaderClickListener{
        void onHeaderLeftClicked();
        void onHeaderCenterClicked();
        void onHeaderRightClicked();
    }

    private OnHeaderClickListener onHeaderClickListener;

    public void setOnHeaderClickListener(OnHeaderClickListener onHeaderClickListener){
        this.onHeaderClickListener = onHeaderClickListener;
    }

    private OnClickListener onClickListener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            int i = v.getId();
            if (i == R.id.header_left) {
                if (onHeaderClickListener != null) onHeaderClickListener.onHeaderLeftClicked();

            } else if (i == R.id.header_center) {
                if (onHeaderClickListener != null) onHeaderClickListener.onHeaderCenterClicked();

            } else if (i == R.id.header_right) {
                if (onHeaderClickListener != null) onHeaderClickListener.onHeaderRightClicked();

            }
        }
    };

}
